<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      db/Redis系列/04-Redis系列之-持久化 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>db/Redis系列/04-Redis系列之-持久化</h2>



  <p class="post-date">2019-12-26</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-持久化的作用"><a href="#一-持久化的作用" class="headerlink" title="一 持久化的作用"></a>一 持久化的作用</h2><h3 id="1-1-什么是持久化"><a href="#1-1-什么是持久化" class="headerlink" title="1.1 什么是持久化"></a>1.1 什么是持久化</h3><p>redis的所有数据保存在内存中，对数据的更新将异步的保存到硬盘上</p>
<h3 id="1-2-持久化的实现方式"><a href="#1-2-持久化的实现方式" class="headerlink" title="1.2 持久化的实现方式"></a>1.2 持久化的实现方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">快照：某时某刻数据的一个完成备份，</span><br><span class="line">	-mysql的Dump</span><br><span class="line">    -redis的RDB</span><br><span class="line">写日志：任何操作记录日志，要恢复数据，只要把日志重新走一遍即可</span><br><span class="line">	-mysql的 Binlog</span><br><span class="line">    -Hhase的 HLog</span><br><span class="line">    -Redis的 AOF</span><br></pre></td></tr></table></figure>

<h2 id="二-RDB"><a href="#二-RDB" class="headerlink" title="二 RDB"></a>二 RDB</h2><h3 id="2-1-什么是RDB"><a href="#2-1-什么是RDB" class="headerlink" title="2.1 什么是RDB"></a>2.1 什么是RDB</h3><p><img src="https://tva1.sinaimg.cn/large/006tNbRwgy1ga9zt9svljj30oo0d644s.jpg" alt="image-20191226120500154"></p>
<h3 id="2-2-触发机制-主要三种方式"><a href="#2-2-触发机制-主要三种方式" class="headerlink" title="2.2 触发机制-主要三种方式"></a>2.2 触发机制-主要三种方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">save(同步)</span></span><br><span class="line"><span class="string">1 客户端执行save命令----》redis服务端----》同步创建RDB二进制文件</span></span><br><span class="line"><span class="string">2 会造成redis的阻塞（数据量非常大的时候）</span></span><br><span class="line"><span class="string">3 文件策略：如果老的RDB存在，会替换老的</span></span><br><span class="line"><span class="string">4 复杂度 o(n)</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">bgsave(异步，Backgroud saving started)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">1 客户端执行save命令----》redis服务端----》异步创建RDB二进制文件（fork函数生成一个子进程（fork会阻塞reids），执行createRDB，执行成功，返回给reids消息）</span></span><br><span class="line"><span class="string">2 此时访问redis，会正常响应客户端</span></span><br><span class="line"><span class="string">3 文件策略：跟save相同，如果老的RDB存在，会替换老的</span></span><br><span class="line"><span class="string">4 复杂度 o(n)</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">自动（通过配置）</span></span><br><span class="line"><span class="string">配置   seconds   changes</span></span><br><span class="line"><span class="string">save   900        1</span></span><br><span class="line"><span class="string">save   300        10</span></span><br><span class="line"><span class="string">save   60         10000</span></span><br><span class="line"><span class="string">如果60s中改变了1w条数据，自动生成rdb</span></span><br><span class="line"><span class="string">如果300s中改变了10条数据，自动生成rdb</span></span><br><span class="line"><span class="string">如果900s中改变了1条数据，自动生成rdb</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">以上三条符合任意一条，就自动生成rdb，内部使用bgsave</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#配置：</span></span><br><span class="line">save <span class="number">900</span> <span class="number">1</span> <span class="comment">#配置一条</span></span><br><span class="line">save <span class="number">300</span> <span class="number">10</span> <span class="comment">#配置一条</span></span><br><span class="line">save <span class="number">60</span> <span class="number">10000</span> <span class="comment">#配置一条</span></span><br><span class="line">dbfilename dump.rdb  <span class="comment">#rdb文件的名字，默认为dump.rdb</span></span><br><span class="line">dir ./ <span class="comment">#rdb文件存在当前目录</span></span><br><span class="line"></span><br><span class="line">stop-writes-on-bgsave-error yes <span class="comment">#如果bgsave出现错误，是否停止写入，默认为yes</span></span><br><span class="line">rdbcompression yes <span class="comment">#采用压缩格式</span></span><br><span class="line">rdbchecksum yes <span class="comment">#是否对rdb文件进行校验和检验</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#最佳配置</span></span><br><span class="line">save <span class="number">900</span> <span class="number">1</span> </span><br><span class="line">save <span class="number">300</span> <span class="number">10</span> </span><br><span class="line">save <span class="number">60</span> <span class="number">10000</span> </span><br><span class="line">dbfilename dump-$&#123;port&#125;.rdb  <span class="comment">#以端口号作为文件名，可能一台机器上很多reids，不会乱</span></span><br><span class="line">dir /bigdiskpath <span class="comment">#保存路径放到一个大硬盘位置目录</span></span><br><span class="line">stop-writes-on-bgsave-error yes <span class="comment">#出现错误停止</span></span><br><span class="line">rdbcompression yes <span class="comment">#压缩</span></span><br><span class="line">rdbchecksum yes <span class="comment">#校验</span></span><br></pre></td></tr></table></figure>

<h3 id="2-3-触发机制-不容忽略的方式"><a href="#2-3-触发机制-不容忽略的方式" class="headerlink" title="2.3 触发机制-不容忽略的方式"></a>2.3 触发机制-不容忽略的方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> 全量复制 <span class="comment">#没有执行save和bgsave没有添加rdb策略，还会生成rdb文件，如果开启主从复制，主会自动生成rdb</span></span><br><span class="line"><span class="number">2</span> debug reload <span class="comment">#debug级别的重启，不会将内存中的数据清空</span></span><br><span class="line"><span class="number">3</span> shutdown save<span class="comment">#关闭会出发rdb的生成</span></span><br></pre></td></tr></table></figure>

<h3 id="2-4-试验"><a href="#2-4-试验" class="headerlink" title="2.4 试验"></a>2.4 试验</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="三-AOF"><a href="#三-AOF" class="headerlink" title="三 AOF"></a>三 AOF</h3><h3 id="3-1-RDB问题"><a href="#3-1-RDB问题" class="headerlink" title="3.1 RDB问题"></a>3.1 RDB问题</h3><p>耗时，耗性能：</p>
<p>不可控，可能会丢失数据</p>
<h3 id="3-2-AOF介绍"><a href="#3-2-AOF介绍" class="headerlink" title="3.2 AOF介绍"></a>3.2 AOF介绍</h3><p>客户端每写入一条命令，都记录一条日志，放到日志文件中，如果出现宕机，可以将数据完全恢复</p>
<h3 id="3-3-AOF的三种策略"><a href="#3-3-AOF的三种策略" class="headerlink" title="3.3 AOF的三种策略"></a>3.3 AOF的三种策略</h3><p>日志不是直接写到硬盘上，而是先放在缓冲区，缓冲区根据一些策略，写到硬盘上</p>
<p>always：redis–》写命令刷新的缓冲区—》每条命令fsync到硬盘—》AOF文件</p>
<p>everysec（默认值）：redis——》写命令刷新的缓冲区—》每秒把缓冲区fsync到硬盘–》AOF文件</p>
<p>no:redis——》写命令刷新的缓冲区—》操作系统决定，缓冲区fsync到硬盘–》AOF文件</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>always</th>
<th>everysec</th>
<th>no</th>
</tr>
</thead>
<tbody><tr>
<td>优点</td>
<td>不丢失数据</td>
<td>每秒一次fsync，丢失1秒数据</td>
<td>不用管</td>
</tr>
<tr>
<td>缺点</td>
<td>IO开销大，一般的sata盘只有几百TPS</td>
<td>丢1秒数据</td>
<td>不可控</td>
</tr>
</tbody></table>
<h3 id="3-4-AOF-重写"><a href="#3-4-AOF-重写" class="headerlink" title="3.4 AOF 重写"></a>3.4 AOF 重写</h3><p>随着命令的逐步写入，并发量的变大， AOF文件会越来越大，通过AOF重写来解决该问题</p>
<table>
<thead>
<tr>
<th>原生AOF</th>
<th>AOF重写</th>
</tr>
</thead>
<tbody><tr>
<td>set hello world<br/>set hello java<br/>set hello hehe<br/>incr counter<br/>incr counter<br/>rpush mylist a<br/>rpush mylist b<br/>rpush mylist c<br/>过期数据</td>
<td>set hello hehe<br/>set counter 2<br/>rpush mylist a b c</td>
</tr>
</tbody></table>
<p>本质就是把过期的，无用的，重复的，可以优化的命令，来优化</p>
<p>这样可以减少磁盘占用量，加速恢复速度</p>
<h4 id="实现方式"><a href="#实现方式" class="headerlink" title="实现方式"></a>实现方式</h4><p>bgrewriteaof：</p>
<p>客户端向服务端发送bgrewriteaof命令，服务端会起一个fork进程，完成AOF重写</p>
<h4 id="AOF重写配置："><a href="#AOF重写配置：" class="headerlink" title="AOF重写配置："></a>AOF重写配置：</h4><table>
<thead>
<tr>
<th>配置名</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>auto-aof-rewrite-min-size</td>
<td>AOF文件重写需要尺寸</td>
</tr>
<tr>
<td>auto-aof-rewrite-percentage</td>
<td>AOF文件增长率</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>统计名</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>aof_current_size</td>
<td>AOF当前尺寸（单位：字节）</td>
</tr>
<tr>
<td>aof_base_size</td>
<td>AOF上次启动和重写的尺寸（单位：字节）</td>
</tr>
</tbody></table>
<p>自动触发时机（两个条件同时满足）：</p>
<p>aof_current_size&gt;auto-aof-rewrite-min-size：当前尺寸大于重写需要尺寸</p>
<p>(aof_current_size-aof_base_size)/aof_base_size&gt;auto-aof-rewrite-percentage:（增长率）当前尺寸减去上次重写的尺寸，除以上次重写的尺寸如果大于配置中的增长率</p>
<h4 id="重写流程"><a href="#重写流程" class="headerlink" title="重写流程"></a>重写流程</h4><p><img src="https://tva1.sinaimg.cn/large/006tNbRwgy1gadsmknx2sj30fy0hw78l.jpg" alt="image-20191229185839519"></p>
<h4 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">appendonly yes <span class="comment">#将该选项设置为yes，打开</span></span><br><span class="line">appendfilename <span class="string">"appendonly-$&#123;port&#125;.aof"</span> <span class="comment">#文件保存的名字</span></span><br><span class="line">appendfsync everysec <span class="comment">#采用第二种策略</span></span><br><span class="line">dir /bigdiskpath <span class="comment">#存放的路径</span></span><br><span class="line">no-appendfsync-on-rewrite yes <span class="comment">#在aof重写的时候，是否要做aof的append操作，因为aof重写消耗性能，磁盘消耗，正常aof写磁盘有一定的冲突，这段期间的数据，允许丢失</span></span><br></pre></td></tr></table></figure>

<h3 id="3-5-AOF-重写演示"><a href="#3-5-AOF-重写演示" class="headerlink" title="3.5 AOF 重写演示"></a>3.5 AOF 重写演示</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="四-RDB和AOF的选择"><a href="#四-RDB和AOF的选择" class="headerlink" title="四 RDB和AOF的选择"></a>四 RDB和AOF的选择</h2><h3 id="4-1-rdb和aof的比较"><a href="#4-1-rdb和aof的比较" class="headerlink" title="4.1 rdb和aof的比较"></a>4.1 rdb和aof的比较</h3><table>
<thead>
<tr>
<th>命令</th>
<th>rdb</th>
<th>aof</th>
</tr>
</thead>
<tbody><tr>
<td>启动优先级</td>
<td>低</td>
<td>高(挂掉重启，会加载aof的数据)</td>
</tr>
<tr>
<td>体积</td>
<td>小</td>
<td>大</td>
</tr>
<tr>
<td>恢复速度</td>
<td>快</td>
<td>慢</td>
</tr>
<tr>
<td>数据安全性</td>
<td>丢数据</td>
<td>根据策略决定</td>
</tr>
<tr>
<td>轻重</td>
<td>重</td>
<td>轻</td>
</tr>
</tbody></table>
<h3 id="4-2-rdb最佳策略"><a href="#4-2-rdb最佳策略" class="headerlink" title="4.2  rdb最佳策略"></a>4.2  rdb最佳策略</h3><p>rdb关掉，主从操作时</p>
<p>集中管理：按天，按小时备份数据</p>
<p>主从配置，从节点打开</p>
<h3 id="4-3-aof最佳策略"><a href="#4-3-aof最佳策略" class="headerlink" title="4.3 aof最佳策略"></a>4.3 aof最佳策略</h3><p>开：缓存和存储，大部分情况都打开，</p>
<p>aof重写集中管理</p>
<p>everysec：通过每秒刷新的策略</p>
<h3 id="4-4-最佳策略"><a href="#4-4-最佳策略" class="headerlink" title="4.4 最佳策略"></a>4.4 最佳策略</h3><p>小分片：每个redis的最大内存为4g</p>
<p>缓存或存储：根据特性，使用不通策略</p>
<p>时时监控硬盘，内存，负载网络等</p>
<p>有足够内存</p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/db/Redis%E7%B3%BB%E5%88%97/03-Redis%E7%B3%BB%E5%88%97%E4%B9%8B-%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95/">
        <span class="nav-arrow">← </span>
        
          db/Redis系列/03-Redis系列之-高级用法
        
      </a>
    
    
      <a class="nav-right" href="/python/%E5%85%B6%E4%BB%96/02-MySQL%E4%B8%BB%E4%BB%8E%E6%90%AD%E5%BB%BA%E5%9F%BA%E4%BA%8Edocker/">
        
          python/其他/02-MySQL主从搭建基于docker
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-持久化的作用"><span class="toc-nav-text">一 持久化的作用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-1-什么是持久化"><span class="toc-nav-text">1.1 什么是持久化</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-2-持久化的实现方式"><span class="toc-nav-text">1.2 持久化的实现方式</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-RDB"><span class="toc-nav-text">二 RDB</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-1-什么是RDB"><span class="toc-nav-text">2.1 什么是RDB</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-2-触发机制-主要三种方式"><span class="toc-nav-text">2.2 触发机制-主要三种方式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-3-触发机制-不容忽略的方式"><span class="toc-nav-text">2.3 触发机制-不容忽略的方式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-4-试验"><span class="toc-nav-text">2.4 试验</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#三-AOF"><span class="toc-nav-text">三 AOF</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-1-RDB问题"><span class="toc-nav-text">3.1 RDB问题</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-2-AOF介绍"><span class="toc-nav-text">3.2 AOF介绍</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-3-AOF的三种策略"><span class="toc-nav-text">3.3 AOF的三种策略</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-4-AOF-重写"><span class="toc-nav-text">3.4 AOF 重写</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#实现方式"><span class="toc-nav-text">实现方式</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#AOF重写配置："><span class="toc-nav-text">AOF重写配置：</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#重写流程"><span class="toc-nav-text">重写流程</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#配置"><span class="toc-nav-text">配置</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-5-AOF-重写演示"><span class="toc-nav-text">3.5 AOF 重写演示</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#四-RDB和AOF的选择"><span class="toc-nav-text">四 RDB和AOF的选择</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-1-rdb和aof的比较"><span class="toc-nav-text">4.1 rdb和aof的比较</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-2-rdb最佳策略"><span class="toc-nav-text">4.2  rdb最佳策略</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-3-aof最佳策略"><span class="toc-nav-text">4.3 aof最佳策略</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-4-最佳策略"><span class="toc-nav-text">4.4 最佳策略</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/db/Redis系列/04-Redis系列之-持久化/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "db/Redis系列/04-Redis系列之-持久化",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>